import * as React from 'react'
export interface Props {
  load: any
  children: (mod: any) => any
}
export interface Store {
  mod: any
}
class Bundle extends React.Component<Props, Store> {
  state = {
    mod: null
  }
  componentWillMount() {
    // console.log(this.props)
    // 加载初始状态
    this.load(this.props)
  }
 
  componentWillReceiveProps(nextProps: any) {
    if (nextProps.load !== this.props.load) {
      this.load(nextProps)
    }
  }
 
  load(props: any) {
    // 重置状态
    this.setState({
      mod: null
    });
    // 传入组件的组件
    props.load((mod: any) => {
      // console.log(mod.default)
      this.setState({
        mod: mod.default ? mod.default : mod
      });
    });
  }
 
  render() {
    // console.log(this.state.mod)
    // 不为空，则渲染传入的子组件函数
    return this.state.mod ? this.props.children(this.state.mod) : null
  }
}
export default Bundle